#!/bin/sh
echo
echo
echo ......................................
echo
echo
# plz change region in file template.yaml and samconfig.toml
# plz change account-id in file template.yaml
# plz change "kr-xniot-sam" to others if needed, (2 in this file, 1 in samconfig.toml)
# plz change Queue setting in template.yaml (arn:aws:sqs:ap-northeast-2:264381684832:espQUEUE)
# aws s3 mb s3://hk-xniot-sam
aws s3 mb s3://kr-xniot-sam
# aws s3 mb s3://eu-xniot-sam
aws iot create-thing-type \
   --thing-type-name "XNEVBK" \
   --thing-type-properties "thingTypeDescription=Created by ShenZhen Xiaoniu Company (www.mosf.cn),searchableAttributes=chargerid,connected,onltime"
qexists=`aws sqs list-queues --output text|grep espQUEUE`
if [ ${#qexists} -eq 0 ]; then
aws sqs create-queue --queue-name espQUEUE --attributes VisibilityTimeout=30
fi

cd dependencies/nodejs
rm -rf node_modules
rm -rf package-lock.json
npm install --save
cd ../..

# 注意修改config.mjs中的OTAS3BUCKET='afr-ota-eu'（2023.09) 需要打开版本控制，不能使用eu-xniot-sam
sam package --template-file template.yaml --s3-bucket kr-xniot-sam --output-template-file espapp.yaml
# sam package --template-file template.yaml --s3-bucket hk-xniot-sam --output-template-file espapp.yaml
sam deploy --template-file ./espapp.yaml --stack-name espapp --capabilities CAPABILITY_IAM
rm espapp.yaml

apiurl=`aws apigatewayv2 get-apis|grep ApiEndpoint|awk -F'\"' '{print $4}'`
sed -i "" "s#export const BASE = '.*';#export const BASE = '$apiurl';#" ../evcharger_awsIoT_vue_gitee/src/config.js
echo ===========================================================
echo $apiurl
echo "This URL used in ../evcharger_awsIoT_vue_gitee/src/config.js"
echo "Goto ../evcharger_awsIoT_vue_gitee/ run ./go.sh for Web apps"
echo ===========================================================
echo 